package com.easytouch.vmhdl.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easytouch.vmhdl.R;

/**
 * Created by gulin on 2017/12/5.
 */
public class BarView extends LinearLayout {

    private TextView mTvLeft;
    private TextView mTvRight;
    private ImageView mIvBar;

    public BarView(Context context) {
        super(context);
        init(context,null, 0);
    }

    public BarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public BarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        View view = inflate(context, R.layout.view_bar, this);
        mTvLeft = view.findViewById(R.id.tv_left);
        mTvRight = view.findViewById(R.id.tv_right);
        mIvBar = view.findViewById(R.id.iv_bar);
    }

    public void setLeftText(final String text)
    {
        mTvLeft.setText(text);
    }

    public void setRightText(final String text)
    {
        mTvRight.setText(text);
    }

    public void setBarDrawable(final int res)
    {
        mIvBar.setImageResource(res);
    }

    public void setBarPercent(final int percent)
    {
        int p = percent;
        if(percent < 0)
        {
            p = 0;
        }
        if(percent > 100)
        {
            p = 100;
        }
        int max = 158;
        int barWidth = 16;
        p = (max - barWidth) * p / 100 + barWidth;
        if(p < barWidth)
        {
            p = barWidth;
        }
        if(p > max)
        {
            p = max;
        }
        ViewGroup.LayoutParams lp =  mIvBar.getLayoutParams();
        lp.width = p;
        lp.height = 10;
        mIvBar.setLayoutParams(lp);
        mIvBar.setMaxHeight(lp.height);
        mIvBar.setMaxWidth(p);
    }
}
